package com.ctsi.system.service;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.system.domain.DictionaryData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典数据 Service 类
 *
 * @author liuhx
 * @description 字典数据
 * @created Fri May 04 15:48:56 CST 2018
 */
@Service
public class DictionaryDataService {
    private static final Logger logger = LoggerFactory.getLogger(DictionaryDataService.class);
    @Autowired
    private MybatisGenericDao dao;

    public void setDao(MybatisGenericDao dao) {
        this.dao = dao;
    }

    /**
     * 添加
     */
    @CacheEvict(value = "dict-data", allEntries = true)
    public void insertDictionaryData(DictionaryData dictionaryData) {
        if (UtilValidate.isEmpty(dictionaryData.getId())) {
            dictionaryData.setId(UUIDGenerator.getId());
        }
        if (null == dictionaryData.getIsdel()) {
            dictionaryData.setIsdel(0);
        }
        logger.debug("insert {}", dictionaryData);
        dao.insert(dictionaryData);
    }

    /**
     * 动态更新
     */
    @CacheEvict(value = "dict-data", allEntries = true)
    public int updateDictionaryDataByPKSelective(DictionaryData dictionaryData) {
        logger.debug("updateByPrimaryKeySelective {}", dictionaryData);
        return dao.update(DictionaryData.class.getName() + ".updateByPrimaryKeySelective", dictionaryData);
    }

    /**
     * 更新
     */
    @CacheEvict(value = "dict-data", allEntries = true)
    public int updateDictionaryData(DictionaryData dictionaryData) {
        logger.debug("update {}", dictionaryData);
        return dao.update(dictionaryData);
    }


    public DictionaryData editDictionaryDataById(String id) {
        if (UtilValidate.isEmpty(id)) {
            return null;
        }
        logger.debug("editById {}", id);
        return (DictionaryData) dao.queryForObject(DictionaryData.class.getName() + ".edit", id);
    }

    /**
     * 根据ID查找 DictionaryData对象
     */
    @Cacheable(value = "dict-data")
    public DictionaryData findDictionaryDataById(String id) {
        if (UtilValidate.isEmpty(id)) {
            return null;
        }
        logger.debug("findById {}", id);
        return dao.get(DictionaryData.class, id);
    }

    @Cacheable(value = "dict-data")
    public List<DictionaryData> findDatasByType(String type) {
        if (UtilValidate.isEmpty(type)) {
            return new ArrayList<>();
        }
        return (List<DictionaryData>) dao.queryForList(DictionaryData.class.getName() + ".selectByType", new HashMap<String, String>() {
            {
                put("type", type);
            }
        });
    }

    /**
     * 删除DictionaryData对象
     */
    @CacheEvict(value = "dict-data")
    public int removeDictionaryData(String id) {
        if (UtilValidate.isNotEmpty(id)) {
            return dao.delete(DictionaryData.class.getName() + ".delete", id);
        }
        //return dao.remove(id);
        return 0;
    }

    /**
     * 详细页查询
     */
    public Map<String, Object> findByDetail(String id) {
        return (Map<String, Object>) dao.queryForObject(DictionaryData.class.getName() + ".findByDetail", id);
    }

    /**
     * 多条件查询结果
     */
    public Page<DictionaryData> findByMulitCondition(Map<String, Object> param, int start, int pageSize) {
        logger.debug("findByMulitCondition {}", param);
        if (pageSize < 0 || pageSize > 2000) {
            pageSize = Page.DEFAULT_PAGE_SIZE;
        }
        if (start != -1) {
            return dao.queryPage(DictionaryData.class.getName() + ".selectMulitCondition", param, start, pageSize);
        } else {
            throw new RuntimeException("参数不正确 !!");
        }
    }

    /**
     * 多条件查询结果不分页
     */
    public List<DictionaryData> find(Map<String, Object> map) {
        logger.debug("find {}", map);
        return (List<DictionaryData>) dao.queryForList(DictionaryData.class.getName() + ".selectByMap", map);
    }

    /**
     * 根据type和code返回字典的value
     *
     * @param type
     * @param code
     * @return
     */
    @Cacheable(value = "dict-data", condition = "#code != null")
    public DictionaryData findDataByTypeAndCode(String type, String code) {
        if (UtilValidate.isEmpty(type) || UtilValidate.isEmpty(code)) {
            return null;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("dictType", type);
        params.put("code", code);

        return (DictionaryData) dao.queryForObject(DictionaryData.class.getName() + ".selectByMap", params);
    }

    /**
     * 从数据库中查询type分类下的所有字典数据
     *
     * @param type 字典数据分类
     * @return List<DictionaryData>
     */
    @Cacheable(value = "dict-data")
    public List<DictionaryData> queryAllDictDataWithoutCache(String type) {
        Map<String, Object> params = new HashMap<String, Object>(2);
        params.put("dictType", type);
        params.put("isOrder", "true");
        return (List<DictionaryData>) dao.queryForList(DictionaryData.class.getName() + ".sel_condition", params);
    }

}
